{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LlamaCloud Page Figure Retrieval\n",
    "This notebook shows an example of retrieving images embedded within a sample PDF document.\n",
    "More docs on using this feature can be found on the [LlamaCloud docs page](https://docs.cloud.llamaindex.ai/llamacloud/retrieval/images)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/sourabhdesai/workspace/llama_index/.venv/bin/python: No module named pip\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install llama-index llama-index-llms-openai llama-cloud-services"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create an Index and upload the figures PDF to it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the LlamaCloud API Key\n",
    "import os\n",
    "\n",
    "api_key = os.environ[\"LLAMA_CLOUD_API_KEY\"]\n",
    "org_id = os.environ.get(\"LLAMA_CLOUD_ORGANIZATION_ID\")\n",
    "openai_api_key = os.environ[\"OPENAI_API_KEY\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/sourabhdesai/workspace/llama_index/docs/examples/llama_cloud\n"
     ]
    }
   ],
   "source": [
    "# print cwd to see where to load PDF file from\n",
    "import os\n",
    "\n",
    "print(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'26a39046-f8d5-46e1-9259-66fa58776f2f'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from llama_cloud.types import LlamaParseParameters\n",
    "from llama_cloud_services import LlamaCloudIndex\n",
    "\n",
    "embedding_config = {\n",
    "    \"type\": \"OPENAI_EMBEDDING\",\n",
    "    \"component\": {\n",
    "        \"api_key\": openai_api_key,\n",
    "        \"model_name\": \"text-embedding-ada-002\",  # You can choose any OpenAI Embedding model\n",
    "    },\n",
    "}\n",
    "\n",
    "index = LlamaCloudIndex.create_index(\n",
    "    name=\"my_index\",\n",
    "    organization_id=org_id,\n",
    "    api_key=api_key,\n",
    "    embedding_config=embedding_config,\n",
    "    llama_parse_parameters=LlamaParseParameters(\n",
    "        take_screenshot=True,\n",
    "        extract_layout=True,\n",
    "    ),\n",
    ")\n",
    "\n",
    "\n",
    "image_figure_slides_path = \"../data/figures/image_figure_slides.pdf\"\n",
    "index.upload_file(\n",
    "    image_figure_slides_path, wait_for_ingestion=True, raise_on_error=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start Retrieving Page Figures\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'file_size': 267370, 'last_modified_at': '2025-06-17T20:52:08', 'file_path': 'image_figure_slides.pdf', 'file_name': 'image_figure_slides.pdf', 'external_file_id': 'image_figure_slides.pdf', 'file_id': '26a39046-f8d5-46e1-9259-66fa58776f2f', 'pipeline_file_id': 'b005a2d5-fb2b-425d-a550-7297793c7410', 'pipeline_id': '75613bd7-5690-4405-9b19-0b0f06e28a04', 'page_label': 2, 'start_page_index': 1, 'start_page_label': 2, 'end_page_index': 1, 'end_page_label': 2, 'document_id': 'e8d76745946110aa1a7190c2a507499cbcf1dc9c19112e1f16', 'start_char_idx': 40, 'end_char_idx': 41, 'page_index': 1, 'figure_name': 'page_1_picture_1.jpg'}\n",
      "Image saved to /var/folders/lw/jz_6fgds7yx0n7_5w0741f_00000gn/T/tmp3l_uiwtb.jpg\n"
     ]
    },
    {
     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQEAyADIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQ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jtgLYVsXT6/3LkRERERERERERERERFxuj6s58EYIUfPtJ+AdHuxZLlujoingotZ2+Az3Wky2KO6wMaSXknsJmgdiThw7HuAVDF0+cHOFQAepwiIg/dEx91VuB6qmPuEQIiY+4T+KIET+KfxRVaQD3GQsvcZOTerOMG41PqjTM5kgdiK42qV59xXwZ7xvHoR5a4DLT37jIP0C7Eb36e392ytGuNOGdltuLSDDUxlkkErTiSN3oS12RkZB8gr//2Q==",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from llama_index.core.schema import ImageNode\n",
    "import base64\n",
    "import tempfile\n",
    "from IPython.display import Image, display\n",
    "\n",
    "retriever = index.as_retriever(\n",
    "    retrieve_page_figure_nodes=True, dense_similarity_top_k=1\n",
    ")\n",
    "\n",
    "nodes = retriever.retrieve(\"Sample query\")\n",
    "\n",
    "image_nodes = [n.node for n in nodes if isinstance(n.node, ImageNode)]\n",
    "\n",
    "for img_node in image_nodes:\n",
    "    print(img_node.metadata)\n",
    "    with tempfile.NamedTemporaryFile(suffix=\".jpg\") as temp_file:\n",
    "        temp_file.write(base64.b64decode(img_node.image))\n",
    "        print(f\"Image saved to {temp_file.name}\")\n",
    "        # Display the image in Jupyter Notebook\n",
    "        display(Image(filename=temp_file.name))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
